<?php
require_once "error-handler.php";
require_once "RtmBot.php";
require_once "Config.php";

// check auth
$auth = isset($_GET["auth"]) ? $_GET["auth"] : "";
if (sha1(Config::$serverSecret . $auth) != Config::$serverSecretSignature) {
    die("Bad auth");
}

$debug = !empty($_REQUEST["debug"]);

try {
    date_default_timezone_set("Europe/Kiev");
    header("Content-Type: text/html; charset=utf-8");
    
    Imified::verboseLevel($debug ? 4 : 2);
    $bot = new RtmBot(Config::$apiKey, Config::$apiSecret, Config::$adminJidHash);
    $bot->processRequest();
}
catch (Exception $e) {
    if ($debug) {
        $message = "Exception: " . $e->getMessage() . " @ " . $e->getFile() . ":" . $e->getLine() . "\n\n" . 
                   "STACK:\n" . $e->getTraceAsString() . "\n";
    }
    else {
        $message = "ERROR: " . $e->getMessage() . "\n";
    }
    echo nl2br(htmlspecialchars($message));
}

function pp($o) {
    echo "<pre>";print_r($o);echo "</pre>";
}